package org.junyi.log.controller;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.junyi.log.service.LogServer;
import org.junyi.pojo.Log;
import org.junyi.pojo.User;
import org.junyi.util.ArchivesLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Component
public class AopLog  
{
 
	@Autowired
	LogServer logServer;
	//private final Logger logger = Logger.getLogger(this.getClass());  
	private static final Logger logger = LogManager.getLogger(AopLog.class);

    private String requestPath = null ; // 请求地址  
    private String userName = "" ; // 用户名  
    private Map<?,?> inputParamMap = null ; // 传入参数  
    private Map<String, Object> outputParamMap = null; // 存放输出结果  
    private long startTimeMillis = 0; // 开始时间  
    private long endTimeMillis = 0; // 结束时间  
    private User user = null;
    private HttpServletRequest request = null;

 
    public  void after(JoinPoint joinPoint) { 
    	//System.out.println("========方法执行后的操作  after============");
        request = getHttpServletRequest(); 
        String targetName = joinPoint.getTarget().getClass().getName();  
        String methodName = joinPoint.getSignature().getName();  
        Object[] arguments = joinPoint.getArgs();  
        Class targetClass = null;
        try {
            targetClass = Class.forName(targetName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }  
        Method[] methods = targetClass.getMethods();
        String operationName = "";
        for (Method method : methods) {  
            if (method.getName().equals(methodName)) {  
                Class[] clazzs = method.getParameterTypes();  
                if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(ArchivesLog.class)!=null) {  
                    operationName = method.getAnnotation(ArchivesLog.class).operationName();
                    break;  
                }  
            }  
        }
        endTimeMillis = System.currentTimeMillis();
        
        //格式化开始时间
        String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
        //格式化结束时间
        String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTimeMillis);
        
if(operationName!=null){
	Log log=new Log();
	log.setCLASS(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()");
	log.setMETHOD(operationName);
	log.setIp(request.getRemoteAddr());
	log.setBeginTime(startTime);
	log.setEndTime(endTime);
	logServer.addLog(log);
}        
    }
 
    public HttpServletRequest getHttpServletRequest(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;  
        HttpServletRequest request = sra.getRequest();
        return request;
    }
}
